package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.SettingPwdActivity;
import com.clzj.makepassenger.bean.UserInfo;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by CLJZ on 2017/6/7.
 */

public class SettingPwdP extends BaseP<SettingPwdActivity>{

    public void setPwd(String password){
        HashMap<String, String> map = new HashMap<>();
        map.put("token",getToken());
        map.put("paymentPass",password);
        String body = Base64Utils.enCode(gson.toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().setPaymentPwd(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().requestFailed(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            modifyUserInfo();
                            getV().setSuccess();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().requestFailed(responseBean.getMsg());
                        }
                    }
                });
    }

    public void modifyPws(String password){
        HashMap<String, String> map = new HashMap<>();
        map.put("token",getToken());
        map.put("paymentPass",password);
        String body = Base64Utils.enCode(gson.toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().modifyPaymentPwd(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().requestFailed(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {

                            getV().modifySuccess();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().requestFailed(responseBean.getMsg());
                        }
                    }
                });
    }

    public void checkPwd(String password){
        HashMap<String, String> map = new HashMap<>();
        map.put("token",getToken());
        map.put("oldPaymentPass",password);
        String body = Base64Utils.enCode(gson.toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().checkPaymentPws(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().requestFailed(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().checkSuccess();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().requestFailed(responseBean.getMsg());
                        }
                    }
                });
    }

    public void modifyUserInfo(){
        UserInfo userInfo = getLocalUserInfo(getToken());
        userInfo.setPayment_password("1");
        daoSession.getUserInfoDao().update(userInfo);
    }

    public void resetPaymentPass(String phone, String verifyCode, String paymentPass) {
        HashMap<String, String> map = new HashMap<>();
        map.put("token",getToken());
        map.put("phone","1" + phone);
        map.put("verify_code",verifyCode);
        map.put("paymentPass",paymentPass);
        String body = Base64Utils.enCode(gson.toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().resetPaymentPassData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().resetSuccess();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().requestFailed(responseBean.getMsg());
                        }
                    }
                });
    }
}
